/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.givebonus.web.GiveBonusAction
   Module Description   :

   Date Created      : 2010/2/4
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.givebonus.web;

import java.io.File;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.givebonus.entity.GiveBonus;
import com.yaodian100.ec.givebonus.service.GiveBonusService;

/**
 * @author feeling.wang
 * 
 */
public class GiveBonusAction extends ActionSupport implements Preparable {
	private final Logger logger = LoggerFactory.getLogger(GiveBonusAction.class);
	private static final long serialVersionUID = 8971276782790288423L;
	private GiveBonusService giveBonusService;
	@Resource(name = "menuService")
	private MenuService menuService;

	private Menu menuBudget;

	private String name;
	private String trxDate;
	private String trxType;
	private String description;
	private String startDate;
	private String expireDate;
	private String startTime;
	private String expireTime;
	private String trxAmount;
	private File giveBonusUpload;
	private String bonusUuid;
	private String uploadFileName;
	private String sendMail;
	private GiveBonus giveBonus;
	private List<GiveBonus> giveBonusList;
	private SimplePager pager;
	private String budget;

	/* 由FORM传入之查询条件 */
	private String paramName;
	private String paramStartDate;
	private String paramEndDate;
	private String paramBudget;

	private String giveBounsResult;

	private Map<String, Object> importResult;

	public String edit() {

		return "edit";
	}

	public String getBonusUuid() {
		return bonusUuid;
	}

	public String getDescription() {
		return description;
	}

	public String getExpireDate() {
		return expireDate;
	}

	public GiveBonus getGiveBonus() {
		return giveBonus;
	}

	public List<GiveBonus> getGiveBonusList() {
		return giveBonusList;
	}

	public File getGiveBonusUpload() {
		return giveBonusUpload;
	}

	public String getName() {
		return name;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getParamName() {
		return paramName;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getTrxAmount() {
		return trxAmount;
	}

	public String getTrxDate() {
		return trxDate;
	}

	public String getTrxType() {
		return trxType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public String getGiveBounsResult() {
		return giveBounsResult;
	}

	public void setGiveBounsResult(String giveBounsResult) {
		this.giveBounsResult = giveBounsResult;
	}

	public void prepare() throws Exception {
		clearActionErrors();
		if (null == pager) {
			pager = new SimplePager();
		}
		if (menuBudget == null) {
			menuBudget = menuService.get("Budget");
		}
	}

	public String save() {
		giveBounsResult = "";
		if (StringUtils.isBlank(budget)) {
			giveBounsResult = "budget_null";
			return "edit";
		}
		try {
			giveBonus = new GiveBonus();
			giveBonus.setDescription(description);
			logger.info("description:{}", description);
			DateFormat df = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
			giveBonus.setExpireDate(df.parse(expireDate + " " + expireTime + ":59"));
			giveBonus.setName(name);
			giveBonus.setBudget(budget);
			giveBonus.setStartDate(df.parse(startDate + " " + startTime + ":00"));
			giveBonus.setTrxAmount(new BigDecimal(0));
			giveBonus.setTrxDate(new Date());
			giveBonus.setTrxType("EVENT_POINT");
			importResult = giveBonusService.saveGiveBonus(giveBonus, giveBonusUpload, uploadFileName, sendMail);
		} catch (Exception e) {
			logger.info("error:{}", e.getMessage());
			giveBounsResult = "wrong_format";
			return "edit";
		}
		if (null != importResult) {
			return "edit";
		} else {
			giveBounsResult = "wrong_format";
			return "edit";
		}
	}

	public String search() throws Exception {
		CommonCriteria criteria = new CommonCriteria();
		criteria.addRlike("name", paramName);

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isNotBlank(paramStartDate)) {
			criteria.addGe("startDate", format.parse(paramStartDate));
		}
		if (StringUtils.isNotBlank(paramEndDate)) {
			criteria.addLe("expireDate", format.parse(paramEndDate));
		}
		if (StringUtils.isNotBlank(paramBudget)) {
			criteria.addEq("budget", paramBudget);
		}
		int totalSize = giveBonusService.getListSize(criteria).intValue();
		pager.setTotalSize(totalSize);
		giveBonusList = giveBonusService.getList(pager.getPageRecord() * pager.getCurrentPage(), pager.getPageRecord(),
				criteria, null);
		return "list";
	}

	public void setBonusUuid(String bonusUuid) {
		this.bonusUuid = bonusUuid;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setExpireDate(String expireDate) {
		this.expireDate = expireDate;
	}

	public void setGiveBonus(GiveBonus giveBonus) {
		this.giveBonus = giveBonus;
	}

	public void setGiveBonusList(List<GiveBonus> giveBonusList) {
		this.giveBonusList = giveBonusList;
	}

	public void setGiveBonusService(GiveBonusService giveBonusService) {
		this.giveBonusService = giveBonusService;
	}

	public void setGiveBonusUpload(File giveBonusUpload) {
		this.giveBonusUpload = giveBonusUpload;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setTrxAmount(String trxAmount) {
		this.trxAmount = trxAmount;
	}

	public void setTrxDate(String trxDate) {
		this.trxDate = trxDate;
	}

	public void setTrxType(String trxType) {
		this.trxType = trxType;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String view() throws Exception {
		giveBonus = giveBonusService.get(bonusUuid);
		return "view";
	}

	public String getParamStartDate() {
		return paramStartDate;
	}

	public void setParamStartDate(String paramStartDate) {
		this.paramStartDate = paramStartDate;
	}

	public String getParamEndDate() {
		return paramEndDate;
	}

	public void setParamEndDate(String paramEndDate) {
		this.paramEndDate = paramEndDate;
	}

	public Map<String, Object> getImportResult() {
		return importResult;
	}

	public void setImportResult(Map<String, Object> importResult) {
		this.importResult = importResult;
	}

	public String getSendMail() {
		return sendMail;
	}

	public void setSendMail(String sendMail) {
		this.sendMail = sendMail;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(String expireTime) {
		this.expireTime = expireTime;
	}

	public Menu getMenuBudget() {
		return menuBudget;
	}

	public void setMenuBudget(Menu menuBudget) {
		this.menuBudget = menuBudget;
	}

	public String getBudget() {
		return budget;
	}

	public void setBudget(String budget) {
		this.budget = budget;
	}

	public String getParamBudget() {
		return paramBudget;
	}

	public void setParamBudget(String paramBudget) {
		this.paramBudget = paramBudget;
	}

}